﻿using System.Collections;
using System.Collections.Generic;

namespace Sidvall.Data
{
    public abstract class SortCriteriaBase<TField> : FilterCriteriaBase<TField>, ISortItemsProvider
    {
        #region Public Members

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public SortItemCollection<TField> SortItems { get; set; }
        public int? RowStartIndex { get; set; }
        public int? RowCount { get; set; }

        #region Clean

        public virtual void Clean(IEnumerable<string> validParameterKeys, IEnumerable<TField> validFilterFieldIds, IEnumerable<TField> validSortFieldIds, bool cleanPaging)
        {
            Clean(validParameterKeys, validFilterFieldIds);
            if (this.SortItems != null)
                this.SortItems.RemoveAllExcept(validSortFieldIds);
            if (cleanPaging)
            {
                this.RowStartIndex = null;
                this.RowCount = null;
            }
        }
        public virtual void Clean(IEnumerable<string> validParameterKeys, IEnumerable<string> validFilterFieldNames, IEnumerable<string> validSortFieldNames, bool cleanPaging)
        {
            Clean(validParameterKeys, validFilterFieldNames);
            if (this.SortItems != null)
                this.SortItems.RemoveAllExcept(validSortFieldNames);
            if (cleanPaging)
            {
                this.RowStartIndex = null;
                this.RowCount = null;
            }
        }

        #endregion
        #region Clear

        public override void Clear()
        {
            base.Clear();
            this.SortItems = null;
            this.RowStartIndex = null;
            this.RowCount = null;
        }

        #endregion

        #region ISortItemsProvider Members

        IList ISortItemsProvider.SortItems
        {
            get
            {
                return this.SortItems;
            }
        }

        #endregion

        #endregion
    }
}
